package weaver.generalledger.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;
import weaver.workflow.workflow.WorkflowComInfo;


/**
 *  总账工单台账.报账公司=总账单.入账公司（隐藏），流程创建后将总账单.
 * 流程编码反写至总账工单台账.总账单单号（发起时）
 */
public class WriteBackDatasAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        writeLog("进入方法 WriteBackDatasAction："+requestInfo);
        RequestManager requestManager = requestInfo.getRequestManager();
        String requestid = requestInfo.getRequestid();
        String workflowid = requestInfo.getWorkflowid();
        String nodetype = requestManager.getNodetype();
        String formId = (new WorkflowComInfo()).getFormId(workflowid);
        String tableName = getNameById(formId);
        String lastoperator = requestInfo.getLastoperator();

        RecordSet recordSet = new RecordSet();
        writeLog("workflowid："+workflowid+"，tableName："+tableName+"，requestid："+requestid+"，formId："+formId);
        String sql="select * from "+tableName+" where requestId=?";
        recordSet.executeQuery(sql, new Object[]{requestid});
        if (recordSet.next()) {
            String zzgdh = recordSet.getString("zzgdh");
            String lcbm = recordSet.getString("lcbm");

            String zzgdtzSql = "select * from uf_zongzhanggongda where lcbm=?"; //根据总账工单号查
            RecordSet zzgdtzRecordSet = new RecordSet();
            zzgdtzRecordSet.executeQuery(zzgdtzSql, new Object[]{zzgdh});
            writeLog(zzgdh);
            if (zzgdtzRecordSet.next()) {
                String hszt = recordSet.getString("hszt");
                String zzgdtzrzgs = zzgdtzRecordSet.getString("rzgs");
                writeLog("hszt:"+hszt+"+zzgdtzhszt:"+zzgdtzrzgs);
                //总账工单台账.报账公司=总账单.入账公司
                if (zzgdtzrzgs.equals(hszt)) {
                    //流程创建后将总账单.流程编码反写至总账工单台账.总账单单号
                    RecordSet zzgdtzSet = new RecordSet();
                    String zzgdtzUpSql = "update uf_zongzhanggongda set zzddh=? where lcbm=?";
                    writeLog(zzgdtzUpSql + zzgdh);
                    zzgdtzSet.executeUpdate(zzgdtzUpSql, lcbm, zzgdh);
                }
            }

        }
        return null;
    }
    public static String getNameById(String var0) {
        RecordSet var1 = new RecordSet();
        String var2 = "";
        String var3 = "select tablename from workflow_bill where id=?";
        var1.executeQuery(var3, new Object[]{var0});
        if (var1.next()) {
            var2 = var1.getString("tablename");
        }

        return var2;
    }
}
